
package com.market.web.action;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;
import org.springframework.beans.BeanUtils;

import com.market.bean.Orders;
import com.market.constant.Constant;
import com.market.web.form.OrderSearchForm;
import com.market.web.form.OrdersForm;
import com.market.web.tag.Pages;
import com.market.web.utils.ListUtils;

public class AdminOrSearchActioin extends AdminOrdersAction
{
    private int perPage = 30;

    private String isprocessed = "";

    private String consignee = "";

    private String address = "";

    private Date fromdate;

    private Date todate;

    private String fromyy;

    private String frommm;

    private String fromdd;

    private String tomm;

    private String toyy;

    private String todd;

    private LabelValueBean[] year;

    private LabelValueBean[] month;

    private LabelValueBean[] day;

    public void reset(ActionMapping arg0, HttpServletRequest arg1)
    {
        year = new LabelValueBean[12];
        month = new LabelValueBean[12];
        day = new LabelValueBean[31];
        int yyyy = Calendar.getInstance().get(Calendar.YEAR);

        for (int i = 0; i < 12; i++)
        {
            year[i] = new LabelValueBean(String.valueOf(yyyy - i), String.valueOf(yyyy - i));
            month[i] = new LabelValueBean(String.valueOf(i + 1), String.valueOf(i + 1));
        }
        for (int i = 0; i < 31; i++)
        {
            day[i] = new LabelValueBean(String.valueOf(i + 1), String.valueOf(i + 1));
        }
    }

    public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1)
    {
        System.out.println("from :" + fromyy + "to:" + toyy);
        if (!"all".equals(fromyy) && !"all".equals(toyy))
        {
            // TODO Auto-generated method stub
            DateFormat f = DateFormat.getDateInstance();
            try
            {
                fromdate = f.parse(fromyy + "-" + frommm + "-" + fromdd);
                todate = f.parse(toyy + "-" + tomm + "-" + todd);
            }
            catch (Exception e)
            {
                // TODO: handle exception
            }
        }
        return null;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public String getConsignee()
    {
        return consignee;
    }

    public void setConsignee(String consignee)
    {
        this.consignee = consignee;
    }

    public Date getFromdate()
    {
        return fromdate;
    }

    public void setFromdate(Date fromdate)
    {
        this.fromdate = fromdate;
    }

    public String getFromdd()
    {
        return fromdd;
    }

    public void setFromdd(String fromdd)
    {
        this.fromdd = fromdd;
    }

    public String getFrommm()
    {
        return frommm;
    }

    public void setFrommm(String frommm)
    {
        this.frommm = frommm;
    }

    public String getFromyy()
    {
        return fromyy;
    }

    public void setFromyy(String fromyy)
    {
        this.fromyy = fromyy;
    }

    public String getIsprocessed()
    {
        return isprocessed;
    }

    public void setIsprocessed(String isprocessed)
    {
        this.isprocessed = isprocessed;
    }

    public Date getTodate()
    {
        return todate;
    }

    public void setTodate(Date todate)
    {
        this.todate = todate;
    }

    public String getTodd()
    {
        return todd;
    }

    public void setTodd(String todd)
    {
        this.todd = todd;
    }

    public String getTomm()
    {
        return tomm;
    }

    public void setTomm(String tomm)
    {
        this.tomm = tomm;
    }

    public String getToyy()
    {
        return toyy;
    }

    public void setToyy(String toyy)
    {
        this.toyy = toyy;
    }

    public LabelValueBean[] getDay()
    {
        return day;
    }

    public void setDay(LabelValueBean[] day)
    {
        this.day = day;
    }

    public LabelValueBean[] getMonth()
    {
        return month;
    }

    public void setMonth(LabelValueBean[] month)
    {
        this.month = month;
    }

    public LabelValueBean[] getYear()
    {
        return year;
    }

    public void setYear(LabelValueBean[] year)
    {
        this.year = year;
    }
    
    public String execute() throws Exception
    {
        OrderSearchForm osf = new OrderSearchForm();
        BeanUtils.copyProperties(this, osf);
        // System.out.println("from date:"+osf.getFromdate().toString());
        int curP = (int)getLong("page");
        if (curP == 0)
        {
            curP++;
        }
        List list = getOrdersService().findByForm(osf, (curP - 1) * perPage, perPage);
        int count = getOrdersService().getCountByForm(osf);
        Pages pages = new Pages();
        pages.setCurPage(curP);
        pages.setPerPage(perPage);
        pages.setRowCount(count);
        StringBuffer path = new StringBuffer();
        path.append(getRequest().getServletPath()).append(".do?isprocessed=").append(osf.getIsprocessed()).append("&consignee=")
                .append(osf.getConsignee()).append("&address=").append(osf.getAddress()).append("&fromyy=").append(
                        osf.getFromyy()).append("&frommm=").append(osf.getFrommm()).append("&fromdd=").append(
                        osf.getFromdd()).append("&toyy=").append(osf.getToyy()).append("&tomm=").append(osf.getTomm())
                .append("&todd=").append(osf.getTodd());
        pages.setPath(path.toString());
        getRequest().setAttribute("orderlist", ListUtils.convert(list, Orders.class, OrdersForm.class));
        getRequest().setAttribute(Constant.PAGES, pages);
        return SUCCESS;
        //return mapping.findForward("orders");
    }
}
